package com.aowin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aowin.dao.UserMapper;
import com.aowin.entity.ResponseResult;
import com.aowin.entity.User;

@RestController
public class UserController {
	@Autowired
	UserMapper usermapper;

	@RequestMapping("/regsterUser")
	public String registerUser(User user) {
		System.out.println(user.getUsername() + " ** " + user.getPassword());
		try {
			if (user.getUsername() == null || user.getPassword() == null) {
				return "账号或密码不能为空";
			} else if ((user.getUsername().matches("1[3578]\\d{9}")
					|| user.getUsername().matches("\\w{2,8}@[0-9a-zA-Z]{3,6}\\.com"))
					&& user.getPassword().matches("[0-9a-zA-Z]{6,10}")) {
				usermapper.registerUser(user);
				return "注册成功";
			} else {
				System.out.println("regex wrong");
				return "注册账号或密码不合法";
			}
		} catch (Exception e) {
			e.printStackTrace();
			return "注册失败";
		}
	}

	@RequestMapping("/login")
	public ResponseResult login(User user) {
		ResponseResult result = new ResponseResult();
		if (user.getUsername() == null || user.getPassword() == null) {
			result.setCode(3);
			result.setMessage("账号或密码不能为空");
			return result;
		}
		User u = usermapper.userLogin(user);
		if (u == null) {
			result.setCode(4);
			result.setMessage("登录失败");
			return result;
		}
		System.out.println(user.getUsername() + "--->" + user.getPassword());
		result.setCode(2);
		result.setMessage("登录成功");
		result.setData(u);
		return result;
	}

	@RequestMapping("/register")
	public int register(User user) {
		System.out.println(user.getUsername() + " ** " + user.getPassword());
		try {
			if (user.getUsername() == null || user.getPassword() == null) {
				return -1;
			} else if ((user.getUsername().matches("1[3578]\\d{9}")
					|| user.getUsername().matches("\\w{2,8}@[0-9 a-z A-Z]{3,6}\\.com"))
					&& user.getPassword().matches("[0-9 a-z A-Z]{8,10}")) {
				return usermapper.registerUser(user);
			} else {
				System.out.println("regex wrong");
				return -1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}

	}
}
